<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const activeTab = ref('login')

const qqloginImage = new URL('@/assets/images/qq-login.png', import.meta.url).href
const vxloginImage = new URL('@/assets/images/vx-login.png', import.meta.url).href
const zfbloginImage = new URL('@/assets/images/zfb-login.png', import.meta.url).href

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: '',
  agreement: false,
})

// 注册表单数据
const registerForm = reactive({
  username: '',
  password: '',
  phone: '',
  agreement: false,
})

// 登录处理函数
const handleLogin = async () => {
  if (!loginForm.agreement) {
    alert('请先同意用户协议和隐私政策')
    return
  }
  try {
    // 这里添加登录逻辑
    console.log('登录表单数据：', loginForm)
    // 登录成功后跳转
    await router.push('/')
  } catch (error) {
    console.error('登录失败：', error)
  }
}

// 注册处理函数
const handleRegister = async () => {
  if (!registerForm.agreement) {
    alert('请先同意用户协议和隐私政策')
    return
  }
  try {
    // 这里添加注册逻辑
    console.log('注册表单数据：', registerForm)
    // 注册成功后跳转
    await router.push('/login')
  } catch (error) {
    console.error('注册失败：', error)
  }
}
</script>

<template>
  <div class="login-sign-page">
    <!-- 登录注册卡片 -->
    <div class="login-sign-card">
      <div class="login-sign-tabs">
        <div :class="{ active: activeTab === 'login' }" @click="activeTab = 'login'">登录</div>
        <div :class="{ active: activeTab === 'register' }" @click="activeTab = 'register'">
          注册
        </div>
      </div>
      <div class="login-sign-content">
        <!-- 登录表单 -->
        <form v-if="activeTab === 'login'" class="login-sign-form" @submit.prevent="handleLogin">
          <label>用户名</label>
          <input v-model="loginForm.username" type="text" placeholder="用户名" />
          <label>密码</label>
          <input v-model="loginForm.password" type="password" placeholder="密码" />
          <div class="login-sign-checkbox-row">
            <input v-model="loginForm.agreement" type="checkbox" id="login-agreement" />
            <label for="login-agreement" class="login-sign-checkbox-label">
              我已阅读和同意 <a href="#">用户协议</a> 和 <a href="#">隐私政策</a>
            </label>
          </div>
          <div class="login-sign-forget">
            <a href="#">忘记密码</a>
          </div>
          <div class="login-sign-other-title">其他登录方式</div>
          <div class="login-sign-third-login">
            <img :src="zfbloginImage" alt="支付宝登录" />
            <img :src="vxloginImage" alt="微信登录" />
            <img :src="qqloginImage" alt="QQ登录" />
          </div>
          <button type="submit">登录</button>
        </form>
        <!-- 注册表单 -->
        <form v-else class="login-sign-form" @submit.prevent="handleRegister">
          <label>用户名</label>
          <input v-model="registerForm.username" type="text" placeholder="用户名" />
          <label>密码</label>
          <input v-model="registerForm.password" type="password" placeholder="密码" />
          <label>手机号码</label>
          <input v-model="registerForm.phone" type="text" placeholder="手机号码" />
          <div class="login-sign-checkbox-row">
            <input v-model="registerForm.agreement" type="checkbox" id="register-agreement" />
            <label for="register-agreement" class="login-sign-checkbox-label">
              我已阅读和同意 <a href="#">用户协议</a> 和 <a href="#">隐私政策</a>
            </label>
          </div>
          <div class="login-sign-other-title">其他登录方式</div>
          <div class="login-sign-third-login">
            <img :src="zfbloginImage" alt="支付宝登录" />
            <img :src="vxloginImage" alt="微信登录" />
            <img :src="qqloginImage" alt="QQ登录" />
          </div>
          <button type="submit">注册</button>
        </form>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-sign-page {
  width: 100vw;
  min-width: 0;
  min-height: 100vh;
  background: #f7f7f7;
  display: flex;
  align-items: center;
  justify-content: center;
  .login-sign-card {
    width: 550px;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 2px 16px rgba(0, 0, 0, 0.08);
    margin: 0 auto;
    padding: 0 0 32px 0;
    transition: min-height 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    min-height: 520px;
    display: flex;
    flex-direction: column;
    .login-sign-tabs {
      width: 100%;
      height: 56px;
      display: flex;
      font-weight: 400;
      border-radius: 12px 12px 0 0;
      overflow: hidden;
      background: #f5f5f5;
      > div {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 22px;
        font-weight: 550;
        color: #222;
        background: #f5f5f5;
        cursor: pointer;
        transition:
          background 0.2s,
          color 0.2s;
        height: 56px;
        &.active {
          background: #fff;
          color: #222;
          border-bottom: 2px solid #fff;
        }
        &:not(.active) {
          color: #888;
        }
      }
    }
    .login-sign-content {
      padding: 0 36px;
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: center;
      .login-sign-form {
        display: flex;
        flex-direction: column;
        margin-top: 10px;
        min-height: 350px;
        transition: min-height 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        label {
          color: #222;
          margin: 15px 0 5px;
          font-size: 16px;
          font-weight: 550;
        }
        input[type='text'],
        input[type='password'] {
          width: 100%;
          height: 44px;
          border-radius: 6px;
          border: 1px solid #d0d0d0;
          padding: 0 14px;
          font-size: 16px;
          font-weight: 550;
          margin-bottom: 8px;
          background: #fafafa;
          transition: border-color 0.2s;
        }
        input[type='text']:focus,
        input[type='password']:focus {
          border-color: #ff6700;
          outline: none;
        }
        .login-sign-checkbox-row {
          display: flex;
          align-items: center;
          color: #888;
          font-size: 14px;
          margin: 10px 0 0 0;
          input[type='checkbox'] {
            width: 16px;
            height: 16px;
            margin-right: 6px;
            cursor: pointer;
            vertical-align: middle;
            position: relative;
            top: 0;
          }
          .login-sign-checkbox-label {
            color: #222;
            font-weight: 400;
            display: flex;
            align-items: center;
            line-height: 1.2;
          }
        }
        .login-sign-forget {
          color: #888;
          font-size: 14px;
        }
        .login-sign-forget a:hover {
          color: #ff6700;
          background: #fff;
        }
        .login-sign-other-title {
          display: block;
          text-align: center;
          color: #888;
          font-size: 15px;
          margin: 10px 0;
        }
        .login-sign-third-login {
          display: flex;
          justify-content: center;
          gap: 45px;
          img {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background: #f5f5f5;
            cursor: pointer;
            transition:
              transform 0.2s,
              box-shadow 0.2s;
            padding: 4px;
            &:hover {
              transform: scale(1.05);
            }
          }
        }
        button {
          width: 100%;
          height: 44px;
          border: none;
          border-radius: 6px;
          background: #222;
          color: #fff;
          font-size: 18px;
          font-weight: 550;
          cursor: pointer;
          margin-top: 18px;
          letter-spacing: 6px;
          transition: background 0.2s;
          &:hover {
            background: #ff6700;
          }
        }
      }
    }
  }
}
@media (max-width: 600px) {
  .login-sign-page {
    .login-sign-card {
      width: 98vw;
      min-width: 0;
      padding: 0 0 24px 0;
      max-width: 100vw;
      .login-sign-content {
        padding: 0 10px;
      }
    }
  }
}
</style>
